package com.xhc.demo.repository;

import com.xhc.demo.entity.Photo;
import com.xhc.demo.entity.Order;
import com.xhc.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface PhotoRepository extends JpaRepository<Photo, Long> {
    List<Photo> findByOrder(Order order);
    List<Photo> findByOrderOrderByCreateTimeDesc(Order order);

    // 获取用户已支付订单的所有照片（分页）
    @Query("SELECT p FROM Photo p WHERE p.order IN (SELECT o FROM Order o WHERE o.user = :user AND o.actualAmount IS NOT NULL AND o.actualAmount > 0) ORDER BY p.createTime DESC")
    Page<Photo> findUserPaidPhotosWithPagination(@Param("user") User user, Pageable pageable);
}
